﻿Public Class Verify
    Inherits System.Web.UI.Page
    Dim DBManager As New DBManager()

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Only run if not postback
        If Not IsPostBack Then
            Dim verificationCode = Request.QueryString("code")
            If (Session("user") IsNot Nothing) Then
                Dim userLoggedin As userAcc = Session("user")
                'Check if user is already verified in the first place
                If (userLoggedin.verified) Then
                    Response.Redirect("Home.aspx")
                Else
                    'Check if there is querystring
                    If (Not verificationCode Is Nothing) Then
                        tbxVerification.Text = verificationCode
                        'Check if user verification code is correct
                        If (DBManager.emailVerification(tbxVerification.Text, userLoggedin.userID)) Then
                            'Verify user in session
                            userLoggedin.verified = True
                            Session("user") = userLoggedin
                            DBManager.verifyUser(userLoggedin.display_name, "true")
                            MsgBox("Thank you for verifying your email, you may now post comments and reviews on GoodAnot", MsgBoxStyle.OkOnly, "Thank you")
                            Response.Redirect("home.aspx")
                        Else
                            'Show error message
                            lblError.Visible = True
                        End If
                    End If
                End If
            Else
                MsgBox("Please login first", MsgBoxStyle.OkOnly, "Please login")
                Response.Redirect("Home.aspx")
            End If
        End If

    End Sub

    Protected Sub btnVerify_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVerify.Click
        Dim verificationCode = Request.QueryString("code")
        If (Session("user") IsNot Nothing) Then
            Dim userLoggedin As userAcc
            If (Session("user") IsNot Nothing) Then
                userLoggedin = Session("user")
                'Check if user is already verified in the first place
                If (userLoggedin.verified) Then
                    Response.Redirect("Home.aspx")
                Else
                    'Check if user verification code is correct
                    If (DBManager.emailVerification(tbxVerification.Text, userLoggedin.userID)) Then
                        'Verify user in session
                        userLoggedin.verified = True
                        Session("user") = userLoggedin
                        DBManager.verifyUser(userLoggedin.display_name, "true")
                        MsgBox("Thank you for verifying your email, you may now post comments and reviews on GoodAnot", MsgBoxStyle.OkOnly, "Thank you")
                        Response.Redirect("home.aspx")
                    End If
                    End If
                End If
            Else
                MsgBox("Please login first", MsgBoxStyle.OkOnly)
            End If
    End Sub
End Class